#include "configfile.h"


void MainWindow::handleSoundSet()
{
    QPoint relativePos = ui->btn_sound->mapToGlobal(QPoint(0, 0));
    ui->sound_widget->move(relativePos.x()-25,relativePos.y()- ui->btn_sound->height()*1.7);
    ui->sound_widget->raise();
    //音乐按钮弹窗设置
    ui->sound_widget->setStyleSheet(
        "QWidget {"
        "   background-color: #FFFFFF;"  // 背景颜色

        "   border-radius: 10px;"        // 圆角半径
        "}"
        );
    //音量控制滑块设置
    ui->sound_slider->setOrientation(Qt::Horizontal);
    ui->sound_slider->setRange(0, 100);   // 设置范围为0-100
    ui->sound_slider->setValue(audioOutput->volume()*100);       // 初始设置为50%
    //联动
    connect(ui->sound_slider, &QSlider::valueChanged, [&](int value) {
        audioOutput->setVolume(value / 100.0);  // 转换为0.0-1.0
        //保存到配置文件
        SaveConfigFile("Sound","volume",QString::number(audioOutput->volume(), 'f', 2));
        ui->sound_value->setText("音量:"+QString::number(value)+"%");
    });
    //静音按钮设置
    connect(ui->mute_btn, &QCheckBox::checkStateChanged, [&]() {
        //bool muted;
        if(ui->mute_btn->isChecked())
        {
            audioOutput->setMuted(true);
            setButtonStyle(ui->btn_sound,":/icons/静音.png","音量控制");
         }
        else
        {
            audioOutput->setMuted(false);
            setButtonStyle(ui->btn_sound,":/icons/声音.png","音量控制");
        }
        int muted=audioOutput->isMuted();
        //保存到配置文件
        SaveConfigFile("Sound","muted",QString::number(muted));
        ui->mute_btn->setText(muted ? "取消静音":"静音");
    });
    if(ui->sound_widget->isHidden())
         ui->sound_widget->show();
    else if(!ui->sound_widget->isHidden())
        ui->sound_widget->hide();

}


